05 Apr 2m read

What is FizzBuzz?

FizzBuzz is a classic programming problem given to developers to review how they structure their code, what best practices they know, etc.

The concept of FizzBuzz is easy: loop all numbers from 1 to 100, all numbers that are dividable by 3 should show Fizz, all numbers dividable by 5 should show Buzz, numbers that are divided by both should thus show FizzBuzz and numbers that are dividable by neither should show the number..

A simple solution to this problem would look as the following code (in TypeScript):

for (let i=1; i<101;i++) {
    let toPrint: number | string = ""; // You could have a default value here.

    if (i%3===0) toPrint += "Fizz"; // Default value + 'Fizz'
    if (i%5===0) toPrint += "Buzz"; // Default value + 'Buzz'

    if (!toPrint) toPrint = i; // If 'toPrint' is emtpy, then show the number

    console.log(toPrint);
}

The code above already gives a lot of information to your interview. It shows that you know:

  • How to use == and === in JavaScript;
  • How to use a union type in TypeScript;
  • How to use let variable (instead of const or var);
  • How to have more compact coding by having 'FizzBuzz' created by two if-cases instead of having a third if-case;
  • ...

The reason this problem gets presented quite a bit is because there are multiple ways to code a solution to this problem and there is no true correct answer. However, the example above is easy to extend upon.

How do I know what is important to solve FizzBuzz?

FizzBuzz, just like many other interview questions, is most of the times slightly altered depending on the company you apply for. Some companies value a quick solution, some value expandability, some value readability, some value simplicity, some value performance, etc.

It's most likely that they present you another problem upon the original problem and depending on what problem they present, you'll have to alter your code. By altering your code, you'll soon notice how good or bad your code is.

For example: imagine our imaginary interviewer asks us to make all hardcoded code easily configurable, then the modified solution would look like this:

const START_NUMBER = 1;
const AMOUNT_OF_NUMBERS_TO_LOOP = 100;
const DEFAULT_PRINTED_VALUE = ""
const PRINT_ON_3 = "Fizz";
const PRINT_ON_5 = "Buzz";

for (let i=START_NUMBER; i<AMOUNT_OF_NUMBERS_TO_LOOP+START_NUMBER;i++) {
    let toPrint: number | string = DEFAULT_PRINTED_VALUE; // You could have a default value here.

    if (i%3===0) toPrint += PRINT_ON_3; // Default value + 'Fizz'
    if (i%5===0) toPrint += PRINT_ON_5; // Default value + 'Buzz'

    if (!toPrint) toPrint = i; // If 'toPrint' is emtpy, then show the number

    console.log(toPrint);
}

The code above shows your interview that you know how to use const | let | var in JavaScript/TypeScript. It also shows what naming conventions you use, how you name your variables, etc.

What other variants of FizzBuzz could an interviewer ask for?

It's most likely you'll never be presented the most basic form of FizzBuzz, you should always expect a slight variant, such as:

  • Recursiveness;
  • Swap Fizz / Buzz;
  • Add 5 new numbers and outputs;
  • Numbers divable by both should only show Fizz;
  • ...

If you notice that it takes quite a lot of work to implement the changes, then your solution could use some refactoring.

A drinking game based on FizzBuzz

Funny enough, the creator of the drinking game platform WorthaShot created a drinking game that was orginally based on FizzBuzz. You can play it here.

Used sources
  1. Spanton, R.. 9/5/2020 How to Solve the FizzBuzz Problem in R Towards data science. https://towardsdatascience.com/how-to-solve-the-fizzbuzz-problem-in-r-c62e7e6c959a. Accessed on 3/1/2021

Cookies. Again...
Do you want cookies?